package com.lanswon.interceptor;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


public class ProtalHandlerInterceptor extends HandlerInterceptorAdapter {

	private static String[] allowArray = new String[] { "admin/", "api/", "phone/", "public/", "file/", "front/",
			"page/" };

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		if (ex != null) {
		}
		super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String url = getRequestUrl(request);
		String[] urlArray = url.split("/");
		boolean result = true;
//		for (String s : urlArray) {
//			if (StringUtils.isBlank(s)) {
//				result = false;
//			}
//		}
//		if (result) {
//			if (isContains(url, allowArray)) {
//
//				// 公共方法通过
//				if (url.startsWith("public/") || url.startsWith("api/weixin/")) {
//					return true;
//				} else if (url.startsWith("admin/")) {
//
//					if (url.startsWith("admin/administrator/login")) {
//						result = true;
//					} else {
//						Object admin = request.getSession().getAttribute(CommonVar.ADMINSESSION);
//
//						if (admin == null) {
//							// TODO 按时标志为true，开发方便
//							result = true;
//							// response.setStatus(401);
//						}
//					}
//				} else if (url.startsWith("page/")) {
//
//					result = true;
//				}
//			}
//		} else {
//			result = false;
//			response.setStatus(401);
//		}
		return result;
	}

	/**
	 * like "/action!method.action"
	 */
	public String getRequestUrl(HttpServletRequest request) {
		String url = request.getRequestURI();
		String path = request.getContextPath() + "/";
//		if (StringUtils.isNotEmpty(path)) {
//			url = url.substring(path.length());
//		}
		url = url.replaceAll("[/\\\\]+", "/");
		if (url.indexOf("/") == 0) {
			url = url.substring(1);
		}
		return url;
	}

	public static boolean isContains(String container, String regx[]) {
		for (int i = 0; i < regx.length; i++) {
			if (container.indexOf(regx[i]) != -1) {
				return true;
			}
		}
		return false;
	}

	public Map<String, String> getWebSiteInfo(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		String url = request.getScheme() + "://";// 请求协议 http 或 https
		url += request.getHeader("host");// 请求服务器
		String webUrl = url;
		url += request.getRequestURI(); // 工程名
//		if (StringUtils.isNotBlank(request.getQueryString())) {
//			url += "?" + request.getQueryString();
//		}
		map.put("url", url);
		map.put("webUrl", webUrl);
		return map;
	}

}
